<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/21
 * Time: 17:26
 */

namespace App\Http\Controllers;


use App\Http\Models\Menu;
use Dotenv\Validator;
use Illuminate\Http\Request;

class MenuController extends Controller
{
    public function lst()
    {
        $menus = Menu::orderBY('id', 'desc')->paginate(10);

        return view('menu/lst', ['menus' => $menus]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            //验证
            $validator = \Validator::make($request->input(),
                //参数一：提交过来的数组，参数二：规则，参数三：规则中文化，参数四：字段中文化
                [
                    'parent_id' => 'required|integer',//多个规则用“|”分隔
                    'order' => 'required|integer',
                    'title' => 'required|min:3',
                    'icon' => 'required',
                    'uri' => 'required',
                ], [
                    'required' => ':attribute 必填',//:attribute为占位符，会替换成下个数组自定义的名称
                    'integer' => ':attribute 必须是整数',
                    'min' => ':attribute 最短字符长度为 :min'
                ], [
                    'parent_id' => '父栏目id',
                    'order' => '排序',
                    'title' => '标题',
                    'icon' => '图标',
                    'uri' => '路径'
                ]
            );
            if ($validator->fails()) { //验证失败
                //withInput可将输入的数据保持
                return redirect()->back()->withErrors($validator)->withInput();
            } else {
                $data = $request->input();
                if (Menu::create($data)) {
                    return redirect('menu/lst')->with('success', '新增加成功');
                } else {
                    return redirect()->back();
                }
            }
        } else {
            $this->get_menus();
            return view('menu/add');
        }
    }

    public function edit(Request $request, $id)
    {
        $item = Menu::find($id);
        if ($request->isMethod('post')) {
            //验证
            $validator = \Validator::make($request->input(),
                //参数一：提交过来的数组，参数二：规则，参数三：规则中文化，参数四：字段中文化
                [
                    'parent_id' => 'required|integer',//多个规则用“|”分隔
                    'order' => 'required|integer',
                    'title' => 'required|min:3',
                    'icon' => 'required',
                    'uri' => 'required',
                ], [
                    'required' => ':attribute 必填',//:attribute为占位符，会替换成下个数组自定义的名称
                    'integer' => ':attribute 必须是整数',
                    'min' => ':attribute 最短字符长度为 :min'
                ], [
                    'parent_id' => '父栏目id',
                    'order' => '排序',
                    'title' => '标题',
                    'icon' => '图标',
                    'uri' => '路径'
                ]
            );
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            } else {
                $data = $request->input();
                if ($item->update($data)) {//注意需用$item调用
                    return redirect('menu/lst')->with('success', '编辑成功');
                } else {
                    return redirect()->back();
                }
            }
        } else {
            return view('menu/edit', ['item' => $item]);
        }
    }

    public function del($id)
    {
        $item = Menu::find($id);
        if ($item->delete()) {
            return redirect('menu/lst')->with('success', '删除成功');
        } else {
            return redirect('menu/lst');
        }
    }

    public function get_menus(){
        $lists=Menu::all()->toArray();
        $arr=array();
        if($lists){
            foreach($lists as $k=>$v){
                $arr[$v['parent_id']][]=$v;//把相同parent_id的数组放在同个数组下
            }
        }

        $tree=array();
        //给所有的父栏目增加一个 child 字段，并将其子栏目放入其中
        foreach($lists as $k=>$v){
            $parent_id=$v['parent_id'];
            if($parent_id===0){//顶级栏目
                $tree[]=$v;
            }else{

            }
        }

        echo "<pre>";
        print_r($arr);
    }
}